//验证令牌中间件
const returned = require('../error_returned/returned')
const jwt = require('jsonwebtoken')
module.exports = (req, res, next) => {
  // 获取传来的·令牌
  if (!req.headers.authorization) {
    returned(res, 3, '无令牌')
    return
  }
  let tokenData
  try {
    tokenData = jwt.verify(req.headers.authorization, 'ASDFWETRRTYU$#^5kjfaklsdhfkjashf');
  } catch (err) {
    returned(res, 4, '令牌是错误的')
    return
  }
  if (tokenData.startTime + tokenData.expires < +new Date()) {
    returned(res, 5, '令牌过期了')
    return
  }
  //保存令牌
  tokenData = req.headers.authorization
  next()
}